import urllib.request as ur
import urllib.parse as up
import json


def fanyi(word):
    # print("开始翻译", word)
    data = {
        'kw': word
    }

    url_param = up.urlencode(data)
    url = 'https://fanyi.baidu.com/sug'
    req = ur.Request(url, data=url_param.encode('utf-8'))
    resp_str = ur.urlopen(req).read()
    resp_json = json.loads(resp_str)
    # print('翻译结束', resp_json)
    return resp_json['data'][0]['v']

while True:
    word = input('请输入要翻译的单词(输入q结束)：')
    if 'q' == word.lower():
        print('翻译结束')
        break
    print(fanyi(word))